#include "config.h"
#include "application.h"
#include "lcd_touch.h"
#include "esp_lcd_touch_cst816s.h"
#include "esp_lvgl_port.h"
#include "esp_lcd_touch.h"
#include <freertos/semphr.h>
#include <esp_log.h>
#include <driver/i2c_master.h>
#include "I2C1.H"

static const char *TAG = "TOUCH";
static esp_lcd_touch_handle_t touch_handle;

// static lv_display_t *lvgl_disp = NULL;
static lv_indev_t *lvgl_touch_indev = NULL;
extern i2c_master_bus_handle_t bus_handle;

static void touch_read_cb(lv_indev_t * indev, lv_indev_data_t * data)
{


    static lv_coord_t last_x = 0;
    static lv_coord_t last_y = 0;
    
    uint16_t touchX[1] = {0};
    uint16_t touchY[1] = {0};
    uint8_t point_num = 0;
    bool touched = false;
    
    // 读取触摸数据
    esp_err_t ret = esp_lcd_touch_read_data(touch_handle);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "触摸数据读取失败: %s", esp_err_to_name(ret));
        data->state = LV_INDEV_STATE_RELEASED;
        return;
    }

    // 获取坐标数据
    touched = esp_lcd_touch_get_coordinates(touch_handle, touchX, touchY, NULL, &point_num, 1);
    
    if(touched && point_num > 0) {
   
        const int x_offset = -20;  // X轴偏移量
        const int y_offset = 0;   // Y轴偏移量
        
        data->point.x = touchX[0] + x_offset;
        data->point.y = touchY[0] + y_offset;
        data->state = LV_INDEV_STATE_PRESSED;
        // ESP_LOGI(TAG, "触摸坐标 原始X:%" PRId32 " Y:%" PRId32 " | 调整后X:%" PRId32 " Y:%" PRId32, 
        //        (int32_t)touchX[0], (int32_t)touchY[0], data->point.x, data->point.y);
    } else {
        data->point.x = last_x;
        data->point.y = last_y;
        data->state = LV_INDEV_STATE_RELEASED;
    }
    
    last_x = data->point.x;
    last_y = data->point.y;
}


bool app_touch_init(void)
{


    const esp_lcd_touch_config_t tp_cfg = {
        .x_max = 240,
        .y_max = 280,
        .rst_gpio_num = GPIO_NUM_NC,
        .int_gpio_num = GPIO_NUM_NC,
        .levels = {
            .reset = 0,
            .interrupt = 0,
        },
        .flags = {
            .swap_xy = 1,
            .mirror_x = 1,
            .mirror_y = 0,
        },
    };

    esp_lcd_panel_io_handle_t touchhandler = NULL;
    const esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_CST816S_CONFIG();
    
    esp_err_t ret = esp_lcd_new_panel_io_i2c(bus_handle, &tp_io_config, &touchhandler);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "New touch panel IO failed: %s", esp_err_to_name(ret));
        return false;  
    }

    ret = esp_lcd_touch_new_i2c_cst816s(touchhandler, &tp_cfg, &touch_handle);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Initialize touch controller failed: %s", esp_err_to_name(ret));
        return false;
    }
    return true;
}

void LVGL_port_add_touch(void){
    ESP_LOGI(TAG, "添加到lvgl触摸驱动");
    if (touch_handle == nullptr) {
        ESP_LOGE(TAG, "Touch handle is null, cannot add touch input");
        return;
    }

    const lvgl_port_touch_cfg_t touch_cfg = {
        .disp = lv_display_get_default(),
        .handle = touch_handle,
    };
    
    lvgl_touch_indev = lvgl_port_add_touch(&touch_cfg);
    if (lvgl_touch_indev == nullptr) {
        ESP_LOGE(TAG, "Failed to add touch input device");
        return;
    }
    
    lv_indev_set_read_cb(lvgl_touch_indev, touch_read_cb);
}

